package com.itheima.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.mapper.CheckgroupMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.dto.CheckGroupDto;
import com.itheima.pojo.vo.CheckGroupVo;
import com.itheima.service.CheckgroupService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class CheckgroupServiceImpl implements CheckgroupService {
    @Autowired
    private CheckgroupMapper checkgroupMapper;
    @Autowired
    private CheckItemMapper checkItemMapper;
    /**
     * 添加检查组
     * @param group
     * @param checkItemIds
     */
    @Override
    public void add(CheckGroupDto group, String checkItemIds) {
        CheckGroup checkGroup = new CheckGroup();
        BeanUtils.copyProperties(group,checkGroup);

        //将字符串checkItemIds转换为Int数组
        String[] split = checkItemIds.split(",");
        List<Integer> list= new ArrayList<>();
        for (int i = 0; i < split.length; i++) {
            list.add(Integer.parseInt(split[i]));
        }
        List<CheckItem> byListid = checkItemMapper.getByListid(list);
        //将List<CheckItem>中的price字段相加赋给checkGroup.price
        BigDecimal price = new BigDecimal(0);
        BigDecimal cost = new BigDecimal(0);
        for (CheckItem checkItem : byListid) {
            price = price.add(checkItem.getPrice());
            cost = cost.add(BigDecimal.valueOf(checkItem.getCost()));
        }
        //将price转成Double类型
        checkGroup.setPrice(price.doubleValue());
        checkGroup.setCost(cost.doubleValue());

        checkGroup.setCreatTime(LocalDateTime.now());
        checkGroup.setUpdateTime(LocalDateTime.now());
        checkgroupMapper.add(checkGroup);
    }

    /**
     * 删除检查组
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        checkgroupMapper.deleteById(id);
    }

    /**
     * 修改检查组
     * @param group
     * @param checkItemIds
     */
    @Override
    public void update(CheckGroupDto group, String checkItemIds) {

        CheckGroup checkGroup = new CheckGroup();
        BeanUtils.copyProperties(group,checkGroup);

        //将字符串checkItemIds转换为Int数组
        String[] split = checkItemIds.split(",");
        List<Integer> list= new ArrayList<>();
        for (int i = 0; i < split.length; i++) {
            list.add(Integer.parseInt(split[i]));
        }
        List<CheckItem> byListid = checkItemMapper.getByListid(list);
        //将List<CheckItem>中的price字段相加赋给checkGroup.price
        BigDecimal price = new BigDecimal(0);
        BigDecimal cost = new BigDecimal(0);
        for (CheckItem checkItem : byListid) {
            price = price.add(checkItem.getPrice());
            cost = cost.add(BigDecimal.valueOf(checkItem.getCost()));
        }
        //将price转成Double类型
        checkGroup.setPrice(price.doubleValue());
        checkGroup.setCost(cost.doubleValue());


        checkGroup.setUpdateTime(LocalDateTime.now());
        checkgroupMapper.update(checkGroup);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<CheckGroupVo> page = checkgroupMapper.findPage(queryPageBean);

        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 查询所有检查组
     * @return
     */
    @Override
    public List<CheckGroupVo> findAll() {
        List<CheckGroupVo> checkGroups = checkgroupMapper.findAll();
        return checkGroups;
    }

    /**
     * 根据id查询检查组
     * @param id
     * @return
     */
    @Override
    public CheckGroupVo getById(Integer id) {
        CheckGroupVo checkGroupVo = checkgroupMapper.getById(id);
        return checkGroupVo;
    }
}
